package work.net.tcp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * 使用TCP发送数据
 */
public class Client {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 9527);
            //读取输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //获取输出流
            PrintWriter pw = new PrintWriter(socket.getOutputStream());
            //从键盘获取输入
            Scanner scanner = new Scanner(System.in);

            while (true) {
                //从控制台获取向服务端发送的消息
                String next = scanner.next();
                pw.println(next);
                pw.flush();

                String s = br.readLine();
                System.out.println("收到服务器响应：" + s);

                if ("再见".equals(next)) {
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
